package com.leo.ktvplayer.service;

import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.ble.ble.BleCallBack;
import com.ble.ble.BleService;
import com.leo.ktvplayer.bluetooth.BleConstant;
import com.leo.ktvplayer.bluetooth.LeProxy;
import com.leo.ktvplayer.bluetooth.bletask.BleEngineFactory;
import com.leo.ktvplayer.bluetooth.bletask.IBleDataEngine;
import com.leo.ktvplayer.ui.setting.BluetoothResultAdapter;
import com.leo.ktvplayer.util.ByteUtils;
import com.leo.ktvplayer.util.ToastUtil;

import java.util.ArrayList;

/**
 * Created by LeoLu on 2017/3/20.
 */

public class KtvplayService extends Service {


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initService();
        return super.onStartCommand(intent, flags, startId);
    }

    private void initService(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(LeProxy.ACTION_GATT_DISCONNECTED);
        filter.addAction(LeProxy.ACTION_RSSI_AVAILABLE);
        filter.addAction(LeProxy.ACTION_DATA_AVAILABLE);
        filter.addAction(LeProxy.ACTION_GATT_CONNECTED);
        filter.addAction(LeProxy.ACTION_GATT_SERVICES_DISCOVERED);

        LocalBroadcastManager.getInstance(this).registerReceiver(connctReceiver,filter);
        bindService(new Intent(this,BleService.class),connect,BIND_AUTO_CREATE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(connctReceiver);
    }

    /**
     * 服务连接的回调
     */
    ServiceConnection connect = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LeProxy.getInstance().setBleService(service);
            Log.d("acthome","bindservice");

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LeProxy.getInstance().setBleService(null);

        }
    };


    /**
     * 广播数据
     */
    BroadcastReceiver connctReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(LeProxy.ACTION_GATT_CONNECTED)) {
                ToastUtil.show("连接成功");
            } else if (intent.getAction().equals(LeProxy.ACTION_DATA_AVAILABLE)){
                byte[] data = intent.getByteArrayExtra(LeProxy.EXTRA_DATA);
                Log.d("acthome","-->" + ByteUtils.byte2String(data));
            }

        }
    };
}
